package com.naizhuai.ui.testunit;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.entity.QRCodeScanBean;
import com.naizhuai.ui.management.CompanyUserDetailsMsgActivity;
import com.naizhuai.ui.task.StepSettingActivity;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.PermissionRequest;
import com.naizhuai.util.PhoneUtils;
import com.yanzhenjie.album.Album;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

/**
 * 扫描二维码的
 */
public class ManageTestScanQRCodeActivity extends BaseActivity implements QRCodeView.Delegate {

    @BindView(R.id.zxingview)
    QRCodeView zXingView;

    private List<String> paramsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPermissionRequest().request(new PermissionRequest.PermissionCallback() {
            @Override
            public void onSuccessful() {
                initQrCode();
            }

            @Override
            public void onFailure() {
                shortToast(getText(R.string.no_camera_permission).toString());
                onBackPressed();
            }
        }, new String[]{Manifest.permission.CAMERA});
    }

    private void initQrCode() {
        zXingView.setDelegate(this);
        zXingView.startSpot();
    }


    @Override
    public int getLayoutId() {
        return R.layout.test_activity_scan_qrcode_layout;
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        NaizhuAILog.e("result:" + result);
        QRCodeScanBean bean=QRCodeScanBean.getScanObject(result);
        if(bean!=null){
            Toast.makeText(this, new Gson().toJson(bean), Toast.LENGTH_SHORT).show();
        }else
            shortToast("扫描错误");
        Intent intent=new Intent();
        Bundle bundle=new Bundle();
        bundle.putSerializable(QRCodeScanBean.SCAN_DATA_KEY,bean);
        intent.putExtras(bundle);
        setResult(QRCodeScanBean.SCAN_CODE,intent);
        finish();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        if (vibrator != null) {
            vibrator.vibrate(200);
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(this, "打开相机出错", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        zXingView.startCamera();
//        mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
        zXingView.showScanRect();
    }

    @Override
    protected void onStop() {
        zXingView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zXingView.onDestroy();
        super.onDestroy();
    }
}
